<?php
/**
 * @author Ngo Minh Tri <minhtri2711@gmail.com>
 */
require_once APPPATH."controllers/cmsbase.php";
class cms_template extends CmsBase {

    var $_require_login = true;
    var $_template_type;
    var $module_name = 'templates';
    var $_assign = array();

    function __construct() {
        parent::__construct();
        $this->load->model('model_c1', 'model_c1');
        $this->load->model('model_nav', 'model_nav');
        // assgin js
        $this->_js_assign('fck_editor_url', public_base_url().'javascripts/fckeditor/');
    }

	/**
	 *
	 *
	 * @return
	 */
	function init($level, $nav_id){
		$this->_level = $level;
		$this->_nav_id = $nav_id;
	}

	/**
	 *
	 *
	 * @return
	 */
    function add($level = 1, $nav_id = 0) {
    	$this->init($level, $nav_id);
        if ($this->input->post('Submit')) {
			$data = parse_input("item_", $_POST);
			$c1_id = $this->model_c1->insert($data);
			//echo $this->db->last_query();
            redirect(site_url(array("c" => "cms_site_content", "m" => "index", "level" => $level, "nav_id" => $nav_id)));
        }
		$nav = array('id' => $nav_id, 'temp' => $this->_template_type);
		$this->_assign['nav'] = $nav;
		$this->_assign['level'] = $level;
		$this->_assign['nav_id'] = $nav_id;
        return $this->_render_my_view($this->_assign);
    }

	/**
	 *
	 *
	 * @return
	 */
    function edit($level = 1, $nav_id = 0, $id = 0) {			
        $this->init($level, $nav_id);
		if(intval($id)){
	        if ($this->input->post('Submit')) {
				$data = parse_input("item_", $_POST);
				$c1_id = $this->model_c1->update($id, $data);				
	            redirect(site_url(array("c" => "cms_site_content", "m" => "index", "level" => $level, "nav_id" => $nav_id)));
	        }
			else{
				$ed = $this->model_c1->find($id);
				$nav = array('id' => $nav_id, 'temp' => $this->_template_type);
				if(!empty($ed)){
					$this->_assign['nav'] = $nav;
					$this->_assign['level'] = $level;
					$this->_assign['nav_id'] = $nav_id;
					$this->_assign['ed'] = $ed;

	        		return $this->_render_my_view($this->_assign);
				}
			}
		}
    }

	/**
	 *
	 *
	 * @return
	 */
    function delete($level = 1, $nav_id = 0, $id = 0) {
    	if(intval($id)){
    		$this->model_c1->delete($id);
			redirect(site_url(array("c" => "cms_site_content", "m" => "index", "level" => $level, "nav_id" => $nav_id)));
		}
    }

	/**
	 *
	 *
	 * @return
	 */
    function _get_common_params() {
        $params = parent::_get_common_params();
        $params['level'] = $this->_level;
        $params['template_type'] = $this->_template_type;
        return $params;
    }

	/**
	 *
	 *
	 * @return
	 */
    function _request2Content($content) {
        $content->setAttributes($_POST);
        if (!isset($_POST['sort']) && $content->getSort() == null) {
            $content->setSort($this->content_model->getLastSort($content->getMain()) + 1);
        }
        return $content;
    }
}
?>
